/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#pragma once

#include "pr_comp.h"			// defs shared with qcc
#include "progdefs.h"			// generated by program cdefs

#define	MAX_STACK_DEPTH		64

// execution stack
typedef struct prstack_s
{
	int	s;
	dfunction_t	*f;
	int *locals;
} prstack_t;

// execution context
typedef struct pr_context_s
{
	dfunction_t	*xfunction;
	int xstatement;
	struct prstack_s *stack;
	int stackdepth;
	BOOL trace;
	int argc;
} pr_context_t;


typedef union eval_s
{
	string_t		string;
	float			_float;
	float			vector[3];
	func_t			function;
	int				_int;
	int				edict;
} eval_t;


struct edict_t
{
	BOOL	free;
	link_t		area;				// linked to a division node or leaf

	int			ed_num;
	int			num_leafs;
	unsigned short	*leafnums;

	entity_state_t	baseline;

	unsigned char	alpha;				// hack to support alpha since it's not part of entvars_t
	BOOL			sendinterval;		// send time until nextthink to client for better lerp timing

	double		freetime;			// sv.time when the object was freed
	entvars_t	v;					// C exported fields from progs

	// DO NOT ADD ANY MEMBERS BEYOND THIS POINT - other fields from progs come immediately after - I WILL HUNT YOU WITH DOGS
};

#define	EDICT_FROM_AREA(l) STRUCT_FROM_LINK(l,edict_t,area)

//============================================================================

typedef struct pr_environment_s
{
	dfunction_t *functions;
	char *strings;
	ddef_t *globaldefs;
	ddef_t *fielddefs;
	dstatement_t *statements;
	globalvars_t *globalStruct;
	int edictSize;
} pr_environment_t;

extern pr_environment_t pr_env;

extern	dprograms_t		*progs;
extern	float			*pr_globals;			// same as pr_env.globalStruct

//============================================================================

void PR_Init (void);

void PR_RunInteraction (func_t fnum, edict_t *self, edict_t *other, float time);
void PR_LoadProgs (void);

void PR_Profile_f (void);

edict_t *ED_Alloc (void);
void ED_Free (edict_t *ed);

char	*ED_NewString (char *string);
// returns a copy of the string allocated from the server's string heap

void ED_Print (edict_t *ed);
void ED_Write (FILE *f, edict_t *ed);
char *ED_ParseEdict (char *data, edict_t *ent);

void ED_WriteGlobals (FILE *f);
void ED_ParseGlobals (char *data);

void ED_LoadFromFile (char *data);

//============================================================================

#define	G_FLOAT(o) (pr_globals[o])
#define	G_INT(o) (*(int *) &pr_globals[o])
#define	G_VECTOR(o) (&pr_globals[o])

#define	G_STRING(o) (PR_DecodeString (*(string_t *) &pr_globals[o]))

#define	G_FUNCTION(o) (*(func_t *) &pr_globals[o])

#define	E_FLOAT(e,o) (((float *) &e->v)[o])
#define	E_INT(e,o) (*(int *) &((float *) &e->v)[o])
#define	E_VECTOR(e,o) (&((float *) &e->v)[o])

#define	E_STRING(e,o) (PR_DecodeString (*(string_t *) &((float*) &e->v)[o]))

extern	int		type_size[8];

typedef void (*builtin_t) (pr_context_t *);
extern	builtin_t *pr_builtins;
extern int pr_numbuiltins;

extern	unsigned short		pr_crc;

void PR_RunError (pr_context_t *context, char *error, ...);

void ED_PrintEdicts (void);
void ED_PrintNum (int ent);

eval_t *GetEdictFieldValue (edict_t *ed, char *field);
#define GETEDICTFIELDVALUE(ed, fieldoffset) (fieldoffset ? (eval_t *) ((byte *) &ed->v + fieldoffset) : NULL)

extern	int	eval_alpha, eval_fullbright;

__inline int PR_EncodeString (char *str)
{
	return str - pr_env.strings;
}

__inline char *PR_DecodeString (int str)
{
	return pr_env.strings + str;
}

